package top.market.controller;


import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.market.model.dto.ProductTypeDTO;
import top.market.model.entity.Product;
import top.market.model.entity.ProductType;
import top.market.model.vo.Result;
import top.market.service.IProductTypeService;

/**
 * <p>
 * 同种商品不同风格(多糖少冰) 前端控制器
 * </p>
 *
 * @author li yuhai
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/product/type")
public class ProductTypeController {

    @Autowired
    IProductTypeService productTypeService;

    @PostMapping("/save")
    public Result save(@RequestBody ProductTypeDTO productTypeDTO) {
        if (productTypeDTO == null) return Result.error("null");
        ProductType productType = new ProductType();
        BeanUtils.copyProperties(productTypeDTO, productType);
        productType.setId(null);
        if (productType.getProductId() == null || productType.getProductId() == 0 ) {
            return Result.error("参数错误");
        }
        if (productType.getProductId() == null || productType.getProductId() == 0 ) {
            return Result.error("参数错误");
        }
        if (StrUtil.isBlank(productType.getName())) {
            return Result.error("参数错误");
        }
        if (StrUtil.isBlank(productType.getValue())) {
            return Result.error("参数错误");
        }
        productTypeService.save(productType);
        return Result.ok("插入成功");
    }

    @PutMapping("/update")
    public Result updateCategory(@RequestBody ProductTypeDTO productTypeDTO) {
        if (productTypeDTO == null) return Result.error("null");
        ProductType productType = new ProductType();
        BeanUtils.copyProperties(productTypeDTO, productType);
        if (productType.getId() == null || productType.getId() == 0 ||
                productType.getProductId() == null || productType.getProductId() == 0
        ) {
            return Result.error("参数错误");
        }
        productTypeService.updateById(productType);
        return Result.ok("更新成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        productTypeService.removeById(id);
        return Result.ok("删除成功");
    }

}
